From 533d938a61d14b68ccea6b9d14d873a64eea59ee Mon Sep 17 00:00:00 2001 From: Jakub Bukaj Date: Sun, 7 Dec 2014 15:25:32 -0500 Subject: [PATCH] Do not require an API token for `cargo search` --- src/registry/lib.rs | 2 +- tests/test_cargo_search.rs | 74 ++++++++++++++++++++++++++++++++++++++ tests/tests.rs | 23 ++++++------ 3 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 tests/test_cargo_search.rs diff --git a/src/registry/lib.rs b/src/registry/lib.rs index 952612bcc..4ff2b2730 100644 --- a/src/registry/lib.rs +++ b/src/registry/lib.rs @@ -194,8 +194,8 @@ impl Registry { .header("Accept", "application/json") .content_type("application/json"); - let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice(); if authorized == Auth::Authorized { + let token = try!(self.token.as_ref().ok_or(Error::TokenMissing)).as_slice(); req = req.header("Authorization", token); } match body { diff --git a/tests/test_cargo_search.rs b/tests/test_cargo_search.rs new file mode 100644 index 000000000..83cebb433 --- /dev/null +++ b/tests/test_cargo_search.rs @@ -0,0 +1,74 @@ +use std::io::{mod, fs, File}; + +use url::Url; + +use cargo::util::{process, ProcessBuilder}; +use support::UPDATING; +use support::{ResultTest, execs, cargo_dir}; +use support::paths; +use support::git::repo; + +use hamcrest::assert_that; + +fn registry_path() -> Path { paths::root().join("registry") } +fn registry() -> Url { Url::from_file_path(®istry_path()).unwrap() } +fn api_path() -> Path { paths::root().join("api") } +fn api() -> Url { Url::from_file_path(&api_path()).unwrap() } + +fn setup() { + let config = paths::root().join(".cargo/config"); + fs::mkdir_recursive(&config.dir_path(), io::USER_DIR).assert(); + File::create(&config).write_str(format!(r#" + [registry] + index = "{reg}" + "#, reg = registry()).as_slice()).assert(); + fs::mkdir_recursive(&api_path().join("api/v1"), io::USER_DIR).assert(); + + repo(®istry_path()) + .file("config.json", format!(r#"{{ + "dl": "{0}", + "api": "{0}" + }}"#, api())) + .build(); +} + +fn cargo_process(s: &str) -> ProcessBuilder { + process(cargo_dir().join("cargo")).unwrap().arg(s) + .cwd(paths::root()) + .env("HOME", Some(paths::home())) +} + +test!(simple { + let crates = api_path().join("api/v1/crates"); + File::create(&crates).write_str(r#"{ + "crates": [{ + "created_at": "2014-11-16T20:17:35Z", + "description": "Design by contract style assertions for Rust", + "documentation": null, + "downloads": 2, + "homepage": null, + "id": "hoare", + "keywords": [], + "license": null, + "links": { + "owners": "/api/v1/crates/hoare/owners", + "reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies", + "version_downloads": "/api/v1/crates/hoare/downloads", + "versions": "/api/v1/crates/hoare/versions" + }, + "max_version": "0.1.1", + "name": "hoare", + "repository": "https://github.com/nick29581/libhoare", + "updated_at": "2014-11-20T21:49:21Z", + "versions": null + }], + "meta": { + "total": 1 + } + }"#).assert(); + + assert_that(cargo_process("search").arg("postgres"), + execs().with_status(0).with_stdout(format!("\ +{updating} registry `[..]` +hoare (0.1.1) Design by contract style assertions for Rust", updating = UPDATING))); +}) diff --git a/tests/tests.rs b/tests/tests.rs index b205bd0e2..a30e08066 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -27,27 +27,28 @@ macro_rules! test( mod test_cargo; mod test_cargo_bench; +mod test_cargo_build_auth; mod test_cargo_build_lib; mod test_cargo_clean; mod test_cargo_compile; mod test_cargo_compile_custom_build; -mod test_cargo_compile_old_custom_build; mod test_cargo_compile_git_deps; +mod test_cargo_compile_old_custom_build; mod test_cargo_compile_path_deps; -mod test_cargo_test; -mod test_shell; -mod test_cargo_cross_compile; -mod test_cargo_run; -mod test_cargo_version; -mod test_cargo_new; mod test_cargo_compile_plugins; +mod test_cargo_cross_compile; mod test_cargo_doc; mod test_cargo_features; +mod test_cargo_fetch; mod test_cargo_freshness; mod test_cargo_generate_lockfile; -mod test_cargo_profiles; +mod test_cargo_new; mod test_cargo_package; -mod test_cargo_build_auth; -mod test_cargo_registry; +mod test_cargo_profiles; mod test_cargo_publish; -mod test_cargo_fetch; +mod test_cargo_registry; +mod test_cargo_run; +mod test_cargo_search; +mod test_cargo_test; +mod test_cargo_version; +mod test_shell; -- 2.30.2